<template>
  <div>图片
    <!--  -->
    <el-upload
      action=""
      list-type="picture-card"
      :file-list="fileList"
      :on-preview="handlePreview"
      :limit="limit"
      :on-remove="handleRemove"
      :class="{disabled:isDisabled}"
      :http-request="handleUpload"
      :on-change="handleChange"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-dialog title="预览" :visible="isShow" @close="isShow=false">
      <img :src="imgUrl" alt="" style="width:100%">
    </el-dialog>
  </div>
</template>

<script>
// SecretId: AKIDZw3tSjlVyA6l8E3bssb3E7j5AyVllUw7

// SecretKey: vax3tRjjTxA5j7E5fiUzDAqFNEp9BwRf
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDZw3tSjlVyA6l8E3bssb3E7j5AyVllUw7', // 身份识别ID
  SecretKey: 'vax3tRjjTxA5j7E5fiUzDAqFNEp9BwRf' // 身份秘钥
})
export default {
  name: 'ImageUpload',
  props: {
    limit: {
      type: Number,
      default: 1
    }
  },

  data() {
    return {
      fileList: [
        // { url: 'https://img0.baidu.com/it/u=2314600222,1003014148&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500' },
        // { url: 'https://hrsass-1309002344.cos.ap-shanghai.myqcloud.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211209204712.jpg' }
      ],
      isShow: false,
      imgUrl: ''
    }
  },
  computed: {
    isDisabled() {
      return this.fileList.length >= this.limit
    },
    isAllUploadSuccess() {
      return this.fileList.every(item => item.status === 'success')
    }

  },
  methods: {
    handlePreview(file) {
      console.log(1111, file)
      this.isShow = true
      this.imgUrl = file.url
    },
    handleRemove(file, fileList) {
      console.log(file, fileList, this.fileList)
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },
    handleUpload({ file }) {
      console.log(file, '说你干脆')
      const fileObj = this.fileList.find(item => item.uid === file.uid)

      fileObj.status = 'uploading'

      cos.putObject({
        Bucket: 'hrsass-1309002344',
        Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 */
        Key: parseInt(Math.random() * 9999) + file.name, /* 文件名 */
        StorageClass: 'STANDARD', // 上传模式, 标准模式
        Body: file, // 上传文件对象
        onProgress: progressData => {
          fileObj.percentage = parseInt(progressData.percent * 100)
        }
      }, (err, data) => {
        // 请求结束后 会触发这个函数
        console.log(err || data)
        if (err) {
          console.log(err)
          return
        }

        fileObj.url = 'https://' + data.Location
        setTimeout(() => {
          fileObj.status = 'success'
        }, 1000)
      })
    },
    handleChange(file, fileList) {
      console.log(file, fileList)
      this.fileList = [...fileList]
    },
    handleBeforeUpload(file) {
      console.log(file)
      const fileSize = file.size / 1024 / 1024 < 2
      if (fileSize) {
        this.$message.error('图片有问题')
        return false
      }
      return true
    }
  }

}
</script>

<style lang="scss" scoped>
.disabled {
  ::v-deep {
    .el-upload--picture-card {
      display: none
    }
  }
}
</style>
